package com.dangf.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dangf.entity.User;
import com.dangf.service.UserService;
import com.dangf.utils.Md5Utils;

/**
 * 登录控制器
 * 
 * @author hais1992
 */
@Controller
@RequestMapping("/login")
public class LoginController extends BaseController {

	@Resource
	private UserService userService;

	@RequestMapping("")
	@ResponseBody
	public Map<String, Object> index(String username, String password) {
		if (username == null || password == null) {
			return setResult(HTTP_CODE_FAILED, "登录失败，请输入账号密码！", "");
		}

		User u = userService.getUserByUsername(username);
		if (u == null) {
			return setResult(HTTP_CODE_FAILED, "登录失败，账号不存在！", "");
		}

		if (password.equals(u.getPassword())) {
			u.setToken(Md5Utils.encryptionFor32(u.getId()+"&"+new Date().getTime()));
			userService.update(u);
			u.setPassword("******");
			return setResult(HTTP_CODE_SUCCESS, "登录成功！", u);
		} else {
			return setResult(HTTP_CODE_FAILED, "登录失败，密码错误！", "");
		}
	}

}
